<?php

extract(extractGet(), EXTR_PREFIX_ALL, 'b');

if(!isset($getResultAsArray)){
    $getResultAsArray = false;
}

if(isset($b_group_id) || $getResultAsArray){
    
    if(!$getResultAsArray){
        $devGroup = R::load('group', $b_group_id);
        if($devGroup->id == 0 && !$getResultAsArray){
            jsonErrorDie("Developer group with provided group id = $b_group_id does not exist.");
        }
    }
    
    if($getResultAsArray){
        
        if(isset($loginUserID) && is_numeric($loginUserID)){
            // Pridobimo vse table glede na user_id
            include "./boardManagement/getUserBoards.php";
        }
        else{
            $boards = R::getAll( 'SELECT * FROM board');
            
        }
    }
    else{
        $boards = R::getAll( 'SELECT * FROM board WHERE group_id = ?', array($b_group_id) );
    }
    
    foreach($boards as &$board){
        $board["projects"] = R::getAll( 'SELECT id, code, name FROM project WHERE board_id = ? AND active = 1', array($board['id']));
        
        foreach ($board["projects"] as &$project){
            
            $baseColumns = R::getAll( 'SELECT DISTINCT name FROM `column` WHERE `board_id` = ? ORDER BY position ASC', array($board['id'])); 
            $columns = array();
            foreach ($baseColumns as $colName){
                $column = array();
                $colName = $colName["name"];
                $subcolumns = R::getAll( 'SELECT * FROM `column` WHERE `board_id` = ? AND `name` = ? ORDER BY position ASC', array($board['id'], $colName));
                
                if(count($subcolumns) == 1 && is_null($subcolumns[0]["subcolumn"])){
                    $column = $subcolumns[0];
                    unset($column["subcolumn"]);
                    $column["cards"] = getCardsForColumn($column["id"], $userOnLogin);
                }
                else{
                    $column["name"] = $colName;
                    foreach($subcolumns as &$subcol){
                        $subcol["name"] = $subcol["subcolumn"];
                        unset($subcol["subcolumn"]);
                        $subcol["cards"] = getCardsForColumn($subcol["id"], $userOnLogin);
                        
                    }
                    $column["subcolumn"] = $subcolumns;
                }
                $columns[] = $column;
                
            }
            $project["columns"] = $columns;
        }
    }
    
    if(!$getResultAsArray){
        jsonSuccessDie("boards", $boards);
    }
    
}
else{
    jsonErrorDie("Not all mandatory parameters set!");
}

function getCardsForColumn($colId, $userOnLogin){
    $cards = R::getAll( 'SELECT * FROM card WHERE column_id = ?' , array($colId));
    foreach($cards as &$card){
        $cardDate = $card["due_date"];
        $xDays = NULL;
        if(isset($userOnLogin) && !is_null($userOnLogin)){
            $xDays = $userOnLogin->reminderdays;
            if($xDays == 0){
                $xDays = NULL;
            }
        }
        if(isLessThanXDaysRemaining($cardDate, $xDays)){
            $card["expiring"] = true;
        }
        else{
            $card["expiring"] = false;
        }
        
        $c = R::load('card', $card["id"]);
        $card["logs"] = array();
        foreach ($c->ownLogList as $log){
            $card["logs"][] = $log->export();
        }
    }
    return $cards;
}
